package com.tron.sync.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tron.sync.entities.SyndaOrg;
import com.tron.sync.mapper.SyndaOrgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SyndaOrgServiceImpl implements SyndaOrgService {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    private SyndaOrgMapper syndaOrgMapper;



    @Override
    public List<SyndaOrg> findByTime(Date date) {
        final List<SyndaOrg> orgs = jdbcTemplate.query("SELECT ipu.ID_ as id,ipu.NAME_ as name ,ipu.ORG_ALIAS_ as orgAlias,ipu.STATUS_ as status,ipu.LEVEL_ID_ as levelId,ipu.ROLE_IDS_ as roleIds,ipu.F_TYPE as type,ipu.F_ATTRIBUTE as attribute,ipu.F_ORG_PID as orgPid,ipu.F_ORGCODE as orgCode,ipu.F_REGION as region,ipu.F_BREVITY as brevity,ipu.F_INHERIT as inherit,ipu.F_SORT as sort,ipu.F_CLASSIFY as classIfy,ipe.PATH_ as path   FROM ibps_party_org ipu LEFT JOIN ibps_party_entity ipe ON ipu.ID_ = ipe.ID_ WHERE ipu.CREATE_TIME_ > ? OR ipu.UPDATE_TIME_ > ?",
                new BeanPropertyRowMapper<>(SyndaOrg.class),
                date,date);
        return orgs;
    }

    @Override
    public List<String> findOrgByType(Integer orgType) {
        List<String> orgIds = null;
        List<SyndaOrg> syndaOrgs = syndaOrgMapper.selectList(new QueryWrapper<SyndaOrg>().eq("F_TYPE",orgType));
        if (!syndaOrgs.isEmpty()){
            orgIds =  syndaOrgs.stream().map(o->o.getId()).collect(Collectors.toList());
        }
        return orgIds;
    }
}
